package studentinformationmanagementsystem.servlet.student;

import studentinformationmanagementsystem.entity.Student;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 苗庆
 * @version 1.0
 * 对于未登陆学生管理系统进行过滤
 */

@WebFilter(urlPatterns = {"/student/function/*","/student1/StudentServlet.do"})
public class StudentFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;

        ServletContext servletContext = request.getServletContext();
        Student student = (Student) req.getSession().getAttribute("Studentlogin");
        //判断当前是否有用户进行了登陆
        if (student != null) {
                chain.doFilter(request, response);  //已经登陆则可以访问其资源
                return;
            }
        //否则就返回到登陆页面
        resp.sendRedirect(req.getContextPath() + "/student/log/Login.html");
    }

    @Override
    public void destroy() {

    }
}
